//
//  ScheduleWeekController.m
//  YunDongGym
//
//  Created by lqq on 2018/8/9.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "ScheduleWeekController.h"
#import "NSDate+Extension.h"
#import "I_Schedule.h"
#import "ScheduleWeekCell.h"
#import "MemberListViewController.h"
#import "ScheduleMemberListController.h"

@interface ScheduleWeekController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UITableView *timeTableView;
@property (nonatomic, strong) UICollectionView *contentCollectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, assign) NSInteger todayLine;
@end

static NSString * const WeekCellReuseIdentifier = @"ScheduleWeekCell";
static NSString * const TimeCellReuseIdentifier = @"TimeCellReuseIdentifier";


@implementation ScheduleWeekController

#pragma mark Class Method
+ (instancetype)controllerWithDate:(NSDate *)date
{
    ScheduleWeekController *vc = [[ScheduleWeekController alloc] init];
    vc.date = date;
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestCoachScheduleInOneWeek];
}

#pragma mark Config/UI
- (void)configUI
{
    _timeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 46, 0) style:UITableViewStylePlain];
    _timeTableView.backgroundColor = [UIColor colorWithHexString:@"F4F6F9"];
    _timeTableView.tableFooterView = [UIView new];
    _timeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _timeTableView.showsVerticalScrollIndicator = NO;
    _timeTableView.insetBottom = 20;
    [self.view addSubview:_timeTableView];
    [_timeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.view);
        make.top.bottom.equalTo(self.view).offset(-14);
        make.width.equalTo(@(46));
    }];
    _timeTableView.delegate = self;
    _timeTableView.dataSource = self;
    
    
    UICollectionViewFlowLayout *dateFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    dateFlowLayout.itemSize = CGSizeMake((kDeviceWidth - 46) / 7,56);
    dateFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    dateFlowLayout.minimumLineSpacing = 0.0;
    dateFlowLayout.minimumInteritemSpacing = 0.0;
    _contentCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:dateFlowLayout];
    _contentCollectionView.insetTop = 28;
    _contentCollectionView.backgroundColor = [UIColor colorWithHexString:@"F4F6F9"];
    _contentCollectionView.insetBottom = 50;
    [self.view addSubview:_contentCollectionView];
    [_contentCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.view);
        make.top.bottom.equalTo(self.view).offset(-14);
        make.left.equalTo(self.timeTableView.mas_right);
    }];
    _contentCollectionView.delegate = self;
    _contentCollectionView.dataSource = self;
    [_contentCollectionView registerClass:[ScheduleWeekCell class] forCellWithReuseIdentifier:WeekCellReuseIdentifier];
    
    if ([_timeTableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        if (@available(iOS 11.0, *)) {
            _timeTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _contentCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
- (void)showEmptyView
{
    [self.view showEmptyWithDesc:@"暂无日程安排" callBackTitle:nil callBack:nil];
}

#pragma mark NET
- (void)requestCoachScheduleInOneWeek
{
    if (self.dataSource.count == 0) {
        [self.view showLoading];
    }
    WeakSelf(self);
    NSArray *dateList = [_date getFirstAndLastDayOfThisWeek];
    NSDate *firstDate = dateList.firstObject;
    
    _todayLine = -1;
    NSDate *current = [NSDate date];
    for (int i = 0 ; i < 7; i++) {
        NSDate *date = [firstDate dateByAddingDays:i];
        if ([date isSameDay:current]) {
            _todayLine = i;
            break;
        }
    }
    
    [I_Schedule requestCoachScheduleInOneWeekWithDate:_date success:^(NSMutableArray *list) {
        weakself.dataSource = list;
        [weakself.contentCollectionView reloadData];
        [weakself.timeTableView reloadData];
        [weakself.view hideStatusView];
        if (weakself.dataSource.count == 0) {
            [weakself showEmptyView];
        }
    } failue:^(NSError *error) {
        if (weakself.dataSource.count == 0) {
            [weakself.view showError:error refreshBlock:^{
                [weakself requestCoachScheduleInOneWeek];
            }];
        }
    }];
    
}

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_Schedule *schdule = _dataSource[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TimeCellReuseIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TimeCellReuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *centerLabel = [cell.contentView addCenterLabelWithText:@"" textColor:HEXColor(@"#9B9B9B") font:RegularFont(11)];
        centerLabel.tag = 100;
        cell.contentView.backgroundColor = TP.controllerBackColor;
    }
    UILabel *centerLabel = [cell.contentView viewWithTag:100];
    centerLabel.text = schdule.timeStr;
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 56;
    
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count * 7;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ScheduleWeekCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WeekCellReuseIdentifier forIndexPath:indexPath];
    NSInteger line = indexPath.item / 7;
    NSInteger lintCount = indexPath.item % 7;
    M_ScheduleWeek *week = _dataSource[line];
    M_Schedule *schedule = [week.scheduleDTOList safeObjectAtIndex:lintCount];

    
//    if (line % 2 == 0) {//偶数行
//        cell.topView.hidden = NO;
//        cell.bottomView.hidden = YES;
//    } else {
//        cell.topView.hidden = YES;
//        cell.bottomView.hidden = YES;
//    }
//    if (line == (_dataSource.count-1)) {
//        cell.bottomView.hidden = NO;
//    }
    if (line == 0) {
        cell.topView.hidden = NO;
    } else {
        cell.topView.hidden = YES;
    }
    if (lintCount > 0) {
        cell.leftView.hidden = YES;
    } else {
        cell.leftView.hidden = NO;
    }

    if (schedule.status) {//状态显示
        cell.backgroundColor = [UIColor colorWithHexString:schedule.status alpha:0.2];
        cell.indicaterView.hidden = NO;
        cell.indicaterView.backgroundColor = [UIColor colorWithHexString:schedule.status];
    } else {
        cell.indicaterView.hidden = YES;
        if (lintCount == _todayLine) {
            cell.backgroundColor = [UIColor colorWithHexString:@"#3AA7FF" alpha:0.2];
        } else {
            cell.backgroundColor = [UIColor clearColor];
        }
    }

    if (schedule.enable) {
        cell.contentLabel.text = @"";
    } else {
        cell.contentLabel.text = schedule.planContent;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger line = indexPath.item / 7;
    NSInteger lintCount = indexPath.item % 7;
    M_ScheduleWeek *week = [_dataSource safeObjectAtIndex:line];
    M_Schedule *schedule = [week.scheduleDTOList safeObjectAtIndex:lintCount];
    ScheduleMemberListController *schduleMemberListVC = [ScheduleMemberListController controllWithDateInt:schedule.dateInt timeInt:schedule.timeInt];
    [self.navigationController pushViewController:schduleMemberListVC animated:YES];
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _contentCollectionView) {
        _timeTableView.contentOffsetY = _contentCollectionView.contentOffsetY + 28;
    } else {
        _contentCollectionView.contentOffsetY = _timeTableView.contentOffsetY - 28;
    }
}


@end
